package com.v1.admin.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.ActionForward;

import weibo4j.model.User;

import com.v1.util.Constant;
import com.v1.web.action.BaseAction;
import com.v1.weibo4j.Weibo4jLO;

/**
 * @author:luyang
 */
public class AdminAction extends BaseAction {

	protected final Log log = LogFactory.getLog(getClass());

	@Override
	protected ActionForward before(HttpServletRequest request, HttpServletResponse response) {
		ActionForward before = super.before(request, response);
		if (before != null) {
			return before;
		}
		// login?
		Weibo4jLO agent = (Weibo4jLO) getBean("Weibo4jLO");
		if (!agent.isLogin()) {
			return goLogin(request);
		}
		// admin?
		User user = agent.getUser();
		String id = user.getId();
		Constant constant = (Constant) getBean("Constant");
		String name = constant.getString(id);
		if (StringUtils.isEmpty(name)) {
			log.warn("uid(" + id + ") is not admin.");
			return ERROR;
		}
		setAttribute(request, "name", name);

		return null;
	}
}
